<?php

namespace App\Http\Middleware;

use App\Common\JwtToken;
use Closure;
use Illuminate\Http\Request;

class CheckApiLogin
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse)  $next
     * @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
     */
    public function handle(Request $request, Closure $next)
    {
        //将token放入header中传输
        $token=$request->header('token');
        if (!$token){
            return response()->json(['code'=>500,'msg'=>'token不存在','data'=>[]]);
        }
        $userId=JwtToken::decodeToken($token);
        if ($userId){
            //将加密id解密传入下一步
            $request->attributes->add(['user_id'=>$userId->user_id]);
        }else{
            return response()->json(['code'=>500,'msg'=>'token过期','data'=>[]]);
        }
        return $next($request);
    }
}
